﻿// <copyright file="PlayerToolbox.xaml.cs" company="Marcin Wosik">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Marcin Wosik</author>
// <email>mawo11@gmail.com</email>
// <date>2010-02-25</date>
// <summary></summary>
namespace GameOfLife.Controls
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using GameOfLife.GameData;
    using GameOfLife.Interfaces;

    public partial class PlayerToolbox : UserControl, IPlayerToolbox, INotifyPropertyChanged
    {
        #region Fields

        private PlayerStatus _Status;

        #endregion Fields

        #region Constructors

        public PlayerToolbox()
        {
            InitializeComponent();
            Status = PlayerStatus.Stopped;
        }

        #endregion Constructors

        #region Events

        public event EventHandler<CommandEventArgs<PlayerCommand>> Command;

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        public PlayerStatus Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
                NotifyPropertyChanged("Status");
                UpdateControls();
            }
        }

        #endregion Properties

        #region Methods

        public void NextStep()
        {
            Status = PlayerStatus.Running;
            FireCommand(PlayerCommand.NextStep);
            Status = PlayerStatus.Paused;
        }

        public void Pause()
        {
            Status = PlayerStatus.Paused;
            FireCommand(PlayerCommand.Pause);
        }

        public void Start()
        {
            Status = PlayerStatus.Running;
            FireCommand(PlayerCommand.Start);
        }

        public void Stop()
        {
            Status = PlayerStatus.Stopped;
            FireCommand(PlayerCommand.Stop);
        }

        private void FireCommand(PlayerCommand command)
        {
            if (Command != null)
            {
                Command(this, new CommandEventArgs<PlayerCommand>(command));
            }
        }

        private void NextStepButtonClick(object sender, RoutedEventArgs e)
        {
            NextStep();
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private void PauseButtonClick(object sender, RoutedEventArgs e)
        {
            Pause();
        }

        private void PlayButtonClick(object sender, RoutedEventArgs e)
        {
            Start();
        }

        private void StopButtonClick(object sender, RoutedEventArgs e)
        {
            Stop();
        }

        private void UpdateControls()
        {
            PlayButton.IsEnabled = Status == PlayerStatus.Stopped || Status == PlayerStatus.Paused;
            PauseButton.IsEnabled = Status == PlayerStatus.Running;
            NextStepButton.IsEnabled = Status == PlayerStatus.Paused;
            StopButton.IsEnabled = Status == PlayerStatus.Running || Status == PlayerStatus.Paused;
        }

        #endregion Methods
    }
}